/*
 * OrderLine.java
 *
 * Created on 27 March 2007, 12:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usyd.comp5348;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;

/**
 * Entity class OrderLine
 *
 *
 * @author Yuezhong Zhang
 */
@Entity(name="Orderline")
@NamedQuery(
     name="getOrderLineByOrderId",
     query="SELECT o FROM Orderline o WHERE o.order= :order"
)
public class OrderLine implements Serializable {

    @ManyToOne(optional=false,cascade={CascadeType.ALL},fetch=FetchType.LAZY)
    @JoinColumn(name="orderid")
    private Order order;
    
    public Order getOrder() {
        return order;
    }
    
    public void setOrder(Order order) {
        this.order = order;
    }
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="OrdLineIdSeq")
    @SequenceGenerator(name="OrdLineIdSeq", sequenceName="ord_line_id_seq")
    private Integer id;
    
    public OrderLine() {
    }
    
    public OrderLine(OrderLineInfo o, Order order) {
        if (o.getCatalogue_id()!=OrderLineInfo.UNDEFINED)
            setCatalogueId(o.getCatalogue_id());
        if (o.getId()!=OrderLineInfo.UNDEFINED)
            setId(o.getId());
        setOrder(order);
        setQuantity(o.getQuantity());
    }
    
    public OrderLineInfo getOrderLineInfo(OrderInfo o) {
        return new OrderLineInfo(getId(), o, getCatalogueId(), getQuantity());
    }
    
    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
    
    /**
     * Determines whether another object is equal to this OrderLine.  The result is
     * <code>true</code> if and only if the argument is not null and is a OrderLine object that
     * has the same id field values as this object.
     *
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof OrderLine)) {
            return false;
        }
        OrderLine other = (OrderLine)object;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
        return true;
    }
    
    /**
     * Returns a string representation of the object.  This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "usyd.comp5348.entity.Orderline[id=" + id + "]";
    }
    
/*    @Column(name="order_id",nullable =false)
    private int orderId;
    
    public int getOrderId() {
        return this.orderId;
    }
    
    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }*/
    
    /**
     * Holds value of property catalogueId.
     */
    @Column(name="catalogue_id",nullable=false)
    private int catalogueId;
    
    /**
     * Getter for property catalogueId.
     * @return Value of property catalogueId.
     */
    public int getCatalogueId() {
        return this.catalogueId;
    }
    
    /**
     * Setter for property catalogueId.
     * @param catalogueId New value of property catalogueId.
     */
    public void setCatalogueId(int catalogueId) {
        this.catalogueId = catalogueId;
    }
    
    /**
     * Holds value of property quantity.
     */
    @Column(name="quantity",nullable = false)
    private int quantity;
    
    /**
     * Getter for property quantity.
     * @return Value of property quantity.
     */
    public int getQuantity() {
        return this.quantity;
    }
    
    /**
     * Setter for property quantity.
     * @param quantity New value of property quantity.
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    
}
